package com.ruoyi.equ.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.EquAssetInfo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.qrCode.QrCodeUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.equ.domain.EquBasic;
import com.ruoyi.equ.service.IEquBasicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 设备基本台账Controller
 * 
 * @author lgq
 * @date 2023-10-07
 */
@Api(value="设备基本信息管理",tags = "设备基本信息接口")
@RestController
@RequestMapping("/equ/basic")
public class EquBasicController extends BaseController
{
    @Autowired
    private IEquBasicService equBasicService;

    /**
     * 查询设备基本台账列表
     */
    @GetMapping("/list")
    @ApiOperation(value="设备基本信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
    })
    public TableDataInfo list(EquBasic equBasic)
    {
        startPage();
        List<EquBasic> list = equBasicService.selectEquBasicList(equBasic);
        for(EquBasic equs : list) {
            if(StringUtils.isNotEmpty(equs.getLabels())) {
                equs.setLabel(equs.getLabels().split(","));
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询所有设备基本信息
     */
    @GetMapping("/listAll")
    @ApiOperation(value="所有设备基本信息")
    public AjaxResult list()
    {
        EquBasic equBasic = new EquBasic();
        List<EquBasic> list = equBasicService.selectEquBasicList(equBasic);
        return AjaxResult.success(list);
    }

    /**
     * 导入模板下载
     * @return
     */
    @GetMapping("/importTemplate")
    @ApiOperation(value="导入模板下载")
    public AjaxResult importTemplate()
    {
        ExcelUtil<EquBasic> util = new ExcelUtil<EquBasic>(EquBasic.class);
        return util.importTemplateExcel("设备台账");
    }

    /**
     * 报告信息导入
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    @ApiOperation(value="报告信息导入")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<EquBasic> util = new ExcelUtil<EquBasic>(EquBasic.class);
        List<EquBasic> equInfoList = util.importExcel(file.getInputStream());
        String message = equBasicService.importEqu(equInfoList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 导出设备基本台账列表
     */
    @Log(title = "设备基本台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value="导出设备基本台账列表")
    public void export(HttpServletResponse response, EquBasic equBasic)
    {
        List<EquBasic> list = equBasicService.selectEquBasicList(equBasic);
        ExcelUtil<EquBasic> util = new ExcelUtil<EquBasic>(EquBasic.class);
        util.exportExcel(response, list, "设备基本台账数据");
    }

    /**
     * 获取设备基本台账详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value="获取设备基本台账详细信息")
    @ApiImplicitParam(paramType="query", name = "id", value = "设备主键", required = true, dataType = "String")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(equBasicService.selectEquBasicById(id));
    }

    /**
     * 首页获取设备基本台账详细信息
     */
    @GetMapping(value = "/getEquInfo/count")
    @ApiOperation(value="首页获取设备基本台账详细信息")
    public AjaxResult getEquInfo()
    {
        return success(equBasicService.selectEquBasicCount());
    }

    /**
     * 根据标签获取设备列表信息
     */
    @PostMapping("/getEquByLabel")
    @ApiOperation(value="根据标签获取设备列表信息")
    @ApiImplicitParam(paramType="query", name = "label", value = "设备标签", required = true, dataType = "String")
    public AjaxResult selectEquBasicByLabels(@RequestBody String label) {
        List<EquBasic> list = equBasicService.selectEquBasicByLabels(label);
        return AjaxResult.success(list);
    }

    /**
     * 根据标签获取设备列表信息后再分组
     */
    @PostMapping("/getEquByLabelGroup")
    @ApiOperation(value="根据标签获取设备列表信息后再分组")
    @ApiImplicitParam(paramType="query", name = "label", value = "设备标签", required = true, dataType = "String")
    public AjaxResult selectEquBasicByLabelGroup(@RequestBody String label) {
        Map<String,  List<EquBasic>> map = equBasicService.selectEquBasicByGroupAndLabel(label);
        return AjaxResult.success(map);
    }

    /**
     * 根据设备标识获取设备基本信息
     */
    @GetMapping(value = "/equNumState/{equNum}/{state}")
    @ApiOperation(value="根据设备标识获取设备基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "equNum", value = "设备编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "state", value = "设备状态", required = true, dataType = "String"),
    })
    public AjaxResult getInfoByEquNumState(@PathVariable("equNum") String equNum, @PathVariable("state") String state)
    {
        return AjaxResult.success(equBasicService.selectEquBasicByEquNum(equNum, state));
    }

    /**
     * 获取设备基本信息详细信息
     */
    @GetMapping(value = "/equNum/{equNum}")
    @ApiOperation(value="获取设备基本信息详细信息")
    @ApiImplicitParam(paramType="query", name = "equNum", value = "设备编号", required = true, dataType = "String")
    public AjaxResult getInfoByEquNum(@PathVariable("equNum") String equNum)
    {
        return AjaxResult.success(equBasicService.selectEquBasicByNum(equNum));
    }

    /**
     * 新增设备基本台账
     */
    @Log(title = "设备基本台账", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value="新增设备基本台账")
    public AjaxResult add(@RequestBody EquBasic equBasic) throws IOException {
        String id = IdUtils.fastSimpleUUID();
        equBasic.setId(id);
        //生成设备信息编码
        String equNum = equBasic.getEquNum();
        String qrPath = RuoYiConfig.getQrPath() + "/" + equNum + ".png";
        EquAssetInfo equInfo = new EquAssetInfo();
        equInfo.setEquNum(equNum);
        equInfo.setEquName(equBasic.getEquName());
        equInfo.setEquModel(equBasic.getEquModel());
        equInfo.setEquManufacturer(equBasic.getEquManufacturer());
        equInfo.setEquPlace(equBasic.getEquPlace());
        equInfo.setQrcodeUrl(equNum);
        List<EquAssetInfo> equInfoList = new ArrayList<>();
        equInfoList.add(equInfo);
        QrCodeUtils qrCodeUtils = new QrCodeUtils();
        qrCodeUtils.messageQrCodeDownload(qrPath, equInfoList);
        equBasic.setQrcode("/profile/qrcode/" + equNum + ".png");
        int ret = equBasicService.insertEquBasic(equBasic);
        return toAjax(ret);
    }

    /**
     * 批量下载设备二维码信息
     */
    @GetMapping("/downEquInfo")
    public void gengerateEquInfo(HttpServletResponse response, @RequestParam("ids") String[] ids) throws IOException {
        //根据设备id获取设备信息列表
        List<EquBasic> equBasicList = equBasicService.selectEquBasicByIds(ids);
        List<EquAssetInfo> equInfoList = new ArrayList<>();
        EquAssetInfo equInfo;
        for(EquBasic equBasic : equBasicList){
            equInfo   = new EquAssetInfo();
            String equNum = equBasic.getEquNum();
            equInfo.setEquNum(equNum);
            equInfo.setEquName(equBasic.getEquName());
            equInfo.setEquModel(equBasic.getEquModel());
            equInfo.setEquManufacturer(equBasic.getEquManufacturer());
            equInfo.setEquPlace(equBasic.getEquPlace());
            equInfo.setQrcodeUrl(equNum);
            equInfoList.add(equInfo);
        }
        QrCodeUtils qrCodeUtils = new QrCodeUtils();
        qrCodeUtils.messageQrCodeDownloadStream(response, equInfoList);
    }

    /**
     * 设备二维码生成
     */
    @Log(title = "设备二维码生成", businessType = BusinessType.INSERT)
    @PostMapping(value = "gengerateQrCode")
    @ApiOperation(value="设备二维码生成")
    public AjaxResult gengerateQrCode(@RequestBody EquBasic equBasic) throws IOException {
        //生成设备信息编码
        String equNum = equBasic.getEquNum();
        String qrPath = RuoYiConfig.getQrPath() + "/" + equNum + ".png";
        EquAssetInfo equInfo = new EquAssetInfo();
        List<EquAssetInfo> equInfoList = new ArrayList<>();
        equInfo.setEquNum(equNum);
        equInfo.setEquName(equBasic.getEquName());
        equInfo.setEquModel(equBasic.getEquModel());
        equInfo.setEquManufacturer(equBasic.getEquManufacturer());
        equInfo.setEquPlace(equBasic.getEquPlace());
        equInfo.setQrcodeUrl(equNum);
        equInfoList.add(equInfo);
        //更新设备二维码数据
        equBasic.setQrcode("/profile/qrcode/" + equNum + ".png");
        int ret = equBasicService.updateEquBasic(equBasic);
        QrCodeUtils qrCodeUtils = new QrCodeUtils();
        qrCodeUtils.messageQrCodeDownload(qrPath, equInfoList);
        return toAjax(ret);
    }

    /**
     * 根据设备id更新设备状态信息
     */
    @GetMapping("/updateStateByIds")
    public AjaxResult updateStateByIds(@RequestParam String[] ids, @RequestParam("state") String state) {
        return AjaxResult.success(equBasicService.updateEquBasics(state, ids));
    }

    /**
     * 修改设备基本台账
     */
    @Log(title = "设备基本台账", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value="修改设备基本台账")
    public AjaxResult edit(@RequestBody EquBasic equBasic)
    {
        return toAjax(equBasicService.updateEquBasic(equBasic));
    }

    /**
     * 根据设备唯一性编号更新设备状态信息
     */
    @GetMapping("/updateStateByEquNum/{equNum}")
    @ApiOperation(value="根据设备唯一性编号更新设备状态信息")
    public AjaxResult updateStateByEquNum(@PathVariable ("equNum") String equNum) {
        EquBasic equBasic = equBasicService.selectEquBasicByNum(equNum);
        equBasic.setEquNum(equNum);
        if(StringUtils.equals("1", equBasic.getState())) {
            equBasic.setState("2");
            return AjaxResult.success(equBasicService.updateEquBasicByNum(equBasic));
        }else if(StringUtils.equals("2", equBasic.getState())) {
            equBasic.setState("1");
            return AjaxResult.success(equBasicService.updateEquBasicByNum(equBasic));
        }
        return AjaxResult.error("设备状态变更失败，检查设备是否正常状态");
    }

    /**
     * 删除设备基本台账
     */
    @ApiOperation(value="删除设备基本台账")
    @Log(title = "设备基本台账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(equBasicService.deleteEquBasicByIds(ids));
    }
}
